package com.example.composestudy.router

import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavGraph
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navigation
import com.example.composestudy.router.part1.WelcomePager
import com.example.composestudy.router.part1.home.HomeDirection2
import com.example.composestudy.router.part1.home.HomePager
import com.example.composestudy.router.part1.login.LoginPager
import com.example.composestudy.router.part2.FirstPage

const val TAG = "RouterPager"

@Preview(showBackground = true)
@Composable
fun RouterPager() {
    val navController = rememberNavController()
    NavHost(
        navController = navController,
        startDestination = RouterConfig.PART_ONE
    ) {
        navigation(
            startDestination = RouterConfig.WELCOME,
            route = RouterConfig.PART_ONE
        ) {
            composable(RouterConfig.WELCOME) {
                WelcomePager(navController)
            }
            composable(RouterConfig.LOGIN) {
                LoginPager(navController)
            }
            composable(
                HomeDirection2.route,
                arguments = HomeDirection2.arguments
            ) {
                val (name, age) = HomeDirection2.parsArguments(it)
                HomePager(navController, name, age)
            }
        }
        navigation(
            startDestination = RouterConfig.FIRST_PAGE,
            route = RouterConfig.PART_TWO
        ) {
            composable(RouterConfig.FIRST_PAGE) {
                FirstPage(navController)
            }
        }
    }
    Log.d(TAG, "RouterPager: 堆栈信息：\n" +
            "backQueue(当前后退堆栈)：${navController.backQueue.size}\n" +
            "destinationCountOnBackStack(后退堆栈上的非导航图目标数): ${
                navController.backQueue.count { entry ->
                    entry.destination !is NavGraph
                }
            }")
}